<template lang="">
    <div>
        <el-form ref="form" :model="form" label-width="80px">
            <el-row :gutter="20">
                <!-- <el-col :span="2">
                    <el-button type="text" icon="el-icon-delete" @click="deleteSelected()">删除选中</el-button>
                </el-col> -->
                <el-col :span="8">
                    <el-form-item label="教师工号:">
                        <el-input v-model="form.teaNum"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="8">
                    <el-form-item label="教师姓名:">
                        <el-input v-model="form.teaName"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" round icon="el-icon-search" @click="toSearch">开始查找</el-button>
                    <el-button type="danger" round icon="el-icon-close" @click="clear">清空</el-button>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>
<script>
    export default {
        name: 'Search',
        data() {
            return {
                form: {
                    teaNum: '',
                    teaName: ''
                }

            }
        },
        methods: {
            toSearch() {
                console.log(this.form);
                if (this.form.teaNum == '') {
                    axios({
                        method: 'GET',
                        url: encodeURI(`/teacher/search/${this.form.teaName}`),
                        withCredentials: true,
                        headers: {
                            'Content-Type': 'application/json;charset=utf-8'
                        }
                    }).then((result) => {
                        let data = result.data.data
                        this.$bus.$emit('search', data)
                        if(Array.isArray(data)){
                            this.$bus.$emit('totalPage',data.length)
                        }else{
                            this.$bus.$emit('totalPage',1)
                        }
                    }).catch((err) => {
                        this.$message({
                            message: err.message,
                            type: 'info'
                        })
                    });
                } else {
                    axios({
                        method: 'GET',
                        url: `/teacher/list/${this.form.teaNum}`,
                        withCredentials: true,
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    }).then((result) => {
                        console.log('teacher/list收到',result.data);
                        let data = result.data.data
                        this.$bus.$emit('search', data)
                        this.$bus.$emit('totalPage',1)
                    }).catch((err) => {
                        this.$message({
                            message: err.message,
                            type: 'info'
                        })
                    });
                }
            },
            clear() {
                this.form = {
                    teaNum: '',
                    teaName: ''
                }
                this.$bus.$emit('clearSearch')
            },
            /* deleteSelected() {
                this.$bus.$emit('deleteSelected')
            } */
        },
        mounted() {
            this.$bus.$on('toSearch', () => {
                this.toSearch()
            })
        },
        beforeDestroy() {
            this.$bus.$off('toSearch')
        },
    }
</script>
<style lang="">

</style>